package towerDefense.libraries;

import java.awt.Dimension;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.ResourceBundle;
import towerDefense.maps.util.LibraryException;
import towerDefense.units.ValuedSprite;
import util.BufferedImageModifier;
import util.libraries.ImageToFileNameLibrary;


public class UnitLibrary extends ImageToFileNameLibrary
{

    private static final String PROJECTILE_PROPERTIES =
        "towerDefense.libraries.units";
    private static final String DEFAULT_DELIMITER = "\\|";
    HashMap<String, ValuedSprite> myUnits;


    public UnitLibrary (File dir)
    {
        super(dir);
    }


    public UnitLibrary (String object, Dimension dimension)
    {
        super(object, dimension);
    }


    public UnitLibrary (String dir)
    {
        super(dir);
    }


    @Override
    public void buildLibrary (File file)
    {
        super.buildLibrary(file);
        myUnits = new HashMap<String, ValuedSprite>();
        ResourceBundle rb = ResourceBundle.getBundle(PROJECTILE_PROPERTIES);
        for (String key : rb.keySet())
            myUnits.put(key, constructUnit((String) rb.getObject(key)));
        System.out.println(myUnits.keySet());
    }


    private ValuedSprite constructUnit (String s)
    {

        String[] info = s.split(DEFAULT_DELIMITER);
        System.out.println(Double.parseDouble(info[1]) * myScalar.height);
        ValuedSprite u =
            new ValuedSprite(BufferedImageModifier.getScaledInstance(getImageByName(info[0]),//img
                                                                     (int) (Double.parseDouble(info[1]) * myScalar.height),
                                                                     (int) (Double.parseDouble(info[2]) * myScalar.width)),
                             Math.sqrt(Math.pow(Double.parseDouble(info[1]) * //rad
                                                myScalar.height, 2) +
                                       Math.pow((Double.parseDouble(info[2]) * myScalar.width),
                                                2)),
                             Double.parseDouble(info[3]) *
                                     Math.sqrt(Math.pow(Double.parseDouble(info[1]) *
                                                                myScalar.height,
                                                        2) +
                                               Math.pow((Double.parseDouble(info[2]) * myScalar.width),
                                                        2)) / 1000,//speed
                             null,//path
                             Double.parseDouble(info[4]),//maxHP
                             Integer.parseInt(info[5]));//worth
        return u;
    }


    public ValuedSprite getUnitByName (String name)
    {
        try
        {
            return (ValuedSprite) myUnits.get(name).clone();
        }
        catch (Exception e)
        {
            throw new LibraryException("Not cloneable");
        }

    }


    public void increaseAllMaxHP (double d, int i)
    {
        for (ValuedSprite vs : myUnits.values())
        {
            vs.setMaxHP(vs.getMaxHP() * d + i);
        }

    }


    public Collection<String> getUnitNameList ()
    {
        return myUnits.keySet();
    }


    public ValuedSprite getRandomUnit ()
    {
        int random = (int) (Math.random() * myUnits.size());
        int i = 0;
        for (String s : myUnits.keySet())
        {
            if (i == random)
            {
                return myUnits.get(s);
            }
            i++;
        }
        return myUnits.get("panda");
    }

}
